package com.abhinav.designPatterns.composite;

import java.util.ArrayList;
import java.util.List;

// Composite
public class CompositeGraphic implements Graphic {
	private List<Graphic> children = new ArrayList<Graphic>();

	@Override
	public void add(Graphic g) {
		children.add(g);
	}

	@Override
	public void remove(Graphic g) {
		if(children.contains(g)){
			children.remove(g);
		}
	}

	@Override
	public Graphic get(int index) {
		if(index < children.size()){
			return children.get(index);
		}
		return null;
	}

	@Override
	public void paint() {
		//run the paint operation for each child
		for(Graphic g : children){
			g.paint();
		}
	}

}
